package org.fyp.synchrona.wsi;

import org.fyp.synchrona.communication.RequestHandler;
import org.fyp.synchrona.util.DataQueue;
import org.fyp.synchrona.util.PacketIn;
import org.fyp.synchrona.util.PacketOut;
import org.fyp.synchrona.util.Test;

import sun.misc.Request;

public class WSInterface {

	private Test test;

	private String name = "";

	public String helloworld(String name) {
		System.out.println("name");
		if (name.equals("kapila")) {
			this.name = "kapila";
		} else {
			for (int i = 0; i < 10000; i++) {
				System.out.println(this.name);
			}
		}
		return "updated";

	}

	public PacketOut openfireInterface(PacketIn inDataOpenFire) {
		if (RequestHandler.returnRunningStatus()) {
			DataQueue.getDataQueueObject().addElement(inDataOpenFire);
			String value = "" + DataQueue.getDataQueueObject().getQueueSize();
			PacketOut outData = new PacketOut();
			outData
					.setExecutionStatus("[INFO] :Element added succesfully...! \n Size of the queue :"
							+ value);
			return outData;
		} else {
			PacketOut outData = new PacketOut();
			outData
					.setExecutionStatus("[INFO] :Please start the service to send data");
			return outData;
		}
	}

	@SuppressWarnings("deprecation")
	public PacketOut adminInterface(PacketIn inDataAdmin) {
		
		if (!(RequestHandler.returnRunningStatus())
				&& inDataAdmin.getAdminCommand().equals("ws -start")) {
			/*to start the service when it is not running*/	
			PacketOut outData = new PacketOut();
			if(RequestHandler.getInterruptStatus()){
				//RequestHandler.setRunningStatus(true);
				//RequestHandler.setInterruptStatus(false);
				RequestHandler.getRequestHandler().restartThread();
				outData
				.setExecutionStatus("[INFO] command you entered "+inDataAdmin.getAdminCommand()+"\n [INFO] :service succesfully restarted!  \n [INFO] :Interrupt Status: "+ RequestHandler.getInterruptStatus() +" \n [INFO] :please enter \"ws -start\" to start the service \n [INFO] :please enter \"ws- stop\" to stop the service \n [INFO] :please enter \"ws -status\" to check the status of the service");
				return outData;
			}
			RequestHandler.setRunningStatus(true);
			RequestHandler.setInterruptStatus(false);
			
			RequestHandler.getRequestHandler().start();
//			}else{
//				outData
//				.setExecutionStatus("[INFO] command you entered "+inDataAdmin.getAdminCommand()+"\n [INFO] :To restart the service, you have to restart the webservice for the momonet! : \n [INFO] :please enter \"ws -start\" to start the service \n [INFO] :please enter \"ws- stop\" to stop the service \n [INFO] :please enter \"ws -status\" to check the status of the service");
//				return outData;
//			}
			outData
					.setExecutionStatus("[INFO] command you entered "+inDataAdmin.getAdminCommand()+"\n [INFO] :service succesfully started! \n [INFO] :Interrupt Status: "+ RequestHandler.getInterruptStatus() +" \n [INFO] :please enter \"ws -start\" to start the service \n [INFO] :please enter \"ws- stop\" to stop the service \n [INFO] :please enter \"ws -status\" to check the status of the service");
			return outData;
		} else if (RequestHandler.returnRunningStatus()
				&& inDataAdmin.getAdminCommand().equals("ws -stop")) {
			/*to stop the service when it is running*/
			
			RequestHandler.getRequestHandler().interruptThread();
			PacketOut outData = new PacketOut();
			outData
					.setExecutionStatus("[INFO] command you entered "+inDataAdmin.getAdminCommand()+"\n [INFO] :service stoped sucessfully ! \n [INFO] :Interrupt Status: "+ RequestHandler.getInterruptStatus() +" \n [INFO] :Service Status "+RequestHandler.returnRunningStatus()+"\n [INFO] :please enter \"ws -start\" to start the service \n [INFO] :please enter \"ws- stop\" to stop the service \n [INFO] :please enter \"ws -status\" to check the status of the service");
			return outData;
		} else if (inDataAdmin.getAdminCommand().equals("ws -status")) {
			/*to get the status when it running or not running*/
			PacketOut outData = new PacketOut();
			outData.setExecutionStatus("[INFO] command you entered "+inDataAdmin.getAdminCommand()+"\n [INFO] :Interrupt Status]"+ RequestHandler.getInterruptStatus() +"\n [INFO] :service status : "
					+ RequestHandler.returnRunningStatus());
			return outData;
		} else if (!(RequestHandler.returnRunningStatus())
				&& inDataAdmin.getAdminCommand().equals("ws -stop")) {
			/*if user sends a command to stop the service when it is not running*/
			PacketOut outData = new PacketOut();
			outData
					.setExecutionStatus("[INFO] command you entered "+inDataAdmin.getAdminCommand()+"\n [INFO] :service is not yet being started \n [INFO] :Interrupt Status: "+ RequestHandler.getInterruptStatus() +" \n [INFO] :please enter \"ws -start\" to start the service \n [INFO] :please enter \"ws- stop\" to stop the service \n [INFO] :please enter \"ws -status\" to check the status of the service");
			return outData;
		} else if (RequestHandler.returnRunningStatus()
				&& inDataAdmin.getAdminCommand().equals("ws -start")) {
			/*if user sends a command to start the service when it is running*/
			PacketOut outData = new PacketOut();
			outData
					.setExecutionStatus("[INFO] command you entered "+inDataAdmin.getAdminCommand()+"\n [INFO] :service is running already \n [INFO] :Interrupt Status: "+ RequestHandler.getInterruptStatus() +" \n [INFO] :please enter \"ws -start\" to start the service \n [INFO] :please enter \"ws- stop\" to stop the service \n [INFO] :please enter \"ws -status\" to check the status of the service");
			return outData;
		} else {
			PacketOut outData = new PacketOut();
			outData
					.setExecutionStatus("[INFO] command you entered "+inDataAdmin.getAdminCommand()+"\n [INFO] :invalid command \n [INFO] :Interrupt Status: "+ RequestHandler.getInterruptStatus() +"\n [INFO] :Service Status "+RequestHandler.returnRunningStatus()+"\n [INFO] :please enter \"ws -start\" to start the service \n [INFO] :please enter \"ws- stop\" to stop the service \n [INFO] :please enter \"ws -status\" to check the status of the service");
			return outData;
		}

	}

}
